//
//  VC_AllTagCourse.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/30.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_AllTagCourse.h"
#import "AllCourseTagView.h"
#import "Cell_Home.h"
#import "VC_Course.h"
#import "VC_Audio.h"


static NSString *identifier = @"all_course_tag_cell_identifier";
static const NSString *DataArrayValueChangeContext;
@interface VC_AllTagCourse ()<UITableViewDelegate, UITableViewDataSource> {
    UITableView *_tableView;
}
@property (nonatomic, strong) AllCourseTagView *headView;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation VC_AllTagCourse
- (AllCourseTagView *)headView {
    if (!_headView) {
        _headView = [AllCourseTagView headView];
        _headView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 150);
        _headView.backgroundColor = [UIColor whiteColor];
    }
    return _headView;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:13];
        label.text = @"没有找到！";
        [label sizeToFit];
        label.center = CGPointMake(_bgView.mj_w * 0.5, _bgView.mj_h * 0.5);
        [_bgView addSubview:label];
    }
    return _bgView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew context:&DataArrayValueChangeContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (context == &DataArrayValueChangeContext) {
        // 设置搜索结果状态
        if (self.dataArray.count == 0) {
            self.bgView.hidden = NO;
        }else {
            self.bgView.hidden = YES;
        }
    }
}
- (void)initTableView {
    // 初始化tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = self.headView;
    _tableView.backgroundView = self.bgView;
    __weak typeof (self) weakself = self;
    [self.headView refreshWith:nil CallBackBlock:^(NSString *key) {
        [weakself reloadTableViewWithFilterDataByKey:key];
    }];
    _tableView.tableFooterView = [UIView new];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Cell_Home class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    [self.view addSubview:_tableView];
}
#pragma mark --mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_Course *model = self.dataArray[indexPath.row];
    Cell_Home *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[Cell_Home alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.tipLabel.text = nil;
    __weak typeof(self) weakself = self;
    [cell refreshWithModel:model CallBackBlock:^(Model_Course *model, CellHomeButtonType type) {
        typeof(weakself) strongself = weakself;
        [strongself homeCellCallBack:model CellHomeButtonType:type];
    }];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 252.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_Course *model = self.dataArray[indexPath.section][indexPath.row];
    [self showCourseDetailWithModel:model];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return self.headView.mj_h;
    }else {
        return CGFLOAT_MIN;
    }
}
#pragma mark --mark 业务逻辑

/**
 点击cell按钮回调
 
 @param model model
 @param type type
 */
- (void)homeCellCallBack:(Model_Course *)model CellHomeButtonType:(CellHomeButtonType)type {
    switch (type) {
        case CellHomeButtonTypeAudio: {
            // 播放音频
            [self showAudioDetailWithModel:model];
            break;
        }
        case CellHomeButtonTypeVedio: {
            // 播放视频
            [self showCourseDetailWithModel:model];
            break;
        }
        default:
            break;
    }
}
- (void)reloadTableViewWithFilterDataByKey:(NSString *)key {
    NSArray *arr = [[CourseManager manager] filterCourseWithCourseLabel:key];
    self.dataArray = [NSArray arrayWithArray:arr];
    [_tableView reloadData];
}
/**
 查看课程详情
 
 @param model model
 */
- (void)showCourseDetailWithModel:(Model_Course *)model {
    VC_Course *vc = [[VC_Course alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 查看音频详情
 
 @param model model
 */
- (void)showAudioDetailWithModel:(Model_Course *)model {
    VC_Audio *vc = [[VC_Audio alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
